package com.sinosoft.dsp.platform.components.portal.iteration;

import java.util.List;

import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

import com.sinosoft.dsp.system.core.log.Log;

public class SinoLazyTreeNode<T extends ISinoTreeDataObject> extends
		DefaultTreeNode {

	private static final long serialVersionUID = 8964276239795150386L;

	private static final Log log = Log.getLog(SinoLazyTreeNode.class.getName());

	private ISinoLazyTreeViewBean<T> vb;

	private boolean hasLoad;

	private List<T> dirList;

	public SinoLazyTreeNode(ISinoLazyTreeViewBean<T> vb) {
		this.vb = vb;
	}

	@Override
	public List<TreeNode> getChildren() {
		if (!hasLoad) {
			load();
			try {
				if (dirList != null)
					vb.addRows(dirList);
			} catch (Exception e) {
				log.error("加载结构数据出现错误");
			}
			hasLoad = true;
		}
		return super.getChildren();
	}

	@Override
	public boolean isLeaf() {
		if (!hasLoad) {
			load();
			if (dirList == null || dirList.size() == 0) {
				return true;
			}
			return false;
		}
		return super.isLeaf();
	}

	@SuppressWarnings("unchecked")
	private void load() {
		if (dirList == null) {
			dirList = vb.loadSubNodes(((T) this.getData()).getId());
		}
	}

}
